package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.WandaEduInfoReqTO;
import com.handydata.antifraud.to.dap.WandaEduInfoResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.ws.WsInvocationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 万达学历信息
 *
 */
@Slf4j
@Service("wandaEduInfoWsService")
public class WandaEduInfoWsServiceImpl extends DataPlatformWsService<WandaEduInfoReqTO, WandaEduInfoResTO> 
		implements WsInvocationService<WandaEduInfoReqTO, WandaEduInfoResTO> {

	@Override
	public WandaEduInfoResTO query(WandaEduInfoReqTO req) throws WsInvokeException {
		this.dataType = DataSourceCode.WANDA_EDU_INFO.getCode();
		return executeQuery(req);
	}

	@Override
	protected String createQueryXml(WandaEduInfoReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>1.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>300502</transCode>");
		xml.append("<channelId>").append("12345").append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<cid>").append(req.getIdCard()).append("</cid>");
		xml.append("<name>").append(req.getName()).append("</name>");
		xml.append("<degree>").append(req.getDegree()).append("</degree>");
		xml.append("</subatm>");
		return xml.toString();
		
	}

	@Override
	protected WandaEduInfoResTO parseResultAndSave(String resultXml, String orderId, Date createDate) {
		log.info("调用万达学历信息接口返回报文：{}", resultXml);
		WandaEduInfoResTO resTO = new WandaEduInfoResTO();
		//解析xml，保存本地DB
		JSONObject jasonObject = JSONObject.parseObject(resultXml);
		if ("0000".equals(jasonObject.get("resCode"))) {

			resTO.setResCode((String)jasonObject.get("resCode"));
			resTO.setResMsg((String)jasonObject.get("resMsg"));
			//resTO.setReqId(dataCallLogsReqBO.getId());
		} else {
			resTO.setResCode("1111");
			resTO.setResMsg("万达学历信息接口返回失败");
		}
		log.info("调用万达学历信息接口返回结果：{}", resTO);
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void register() {
		registerToWsServiceFactory(this, WandaEduInfoReqTO.class);
	}

	
}

